package com.ship.contract.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ship.common.security.utils.SecurityUtils;
import com.ship.contract.bean.*;
import com.ship.contract.mapper.BpmJsonModelMapper;
import com.ship.contract.service.*;
import com.ship.system.api.BpmInstanceService;
import com.ship.system.api.ConBpmResultService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@Validated
public class ConBpmResultApi implements ConBpmResultService {

    @Resource
    BpmJsonModelMapper bpmJsonModelMapper;

    @Resource
    ConExcellentCasesService conExcellentCasesService;

    @Resource
    ConResourceMemorandumService conResourceMemorandumService;

    @Resource
    ConShippingService conShippingService;

    @Resource
    ConShippingAgencyService conShippingAgencyService;

    @Resource
    ConBrokerService conBrokerService;

    @Resource
    ConThirdPartyMeasurementService conThirdPartyMeasurementService;

    @Resource
    ConInsuranceGoodsService conInsuranceGoodsService;

    @Resource
    ConRefuelingService conRefuelingService;

    @Resource
    ConLawyerService conLawyerService;

    @Resource
    ConTemplateService conTemplateService;

    @Resource
    ConTemplateClauseService conTemplateClauseService;

    @Resource
    ConInsurancePeopleService conInsurancePeopleService;
    @Resource
    ConResourceMainService conResourceMainService;
    @Override
    @Transactional
    public void updateConResult(String bussinedId, Integer result,String bpmKey) {
        if(bpmKey.equals(BpmInstanceService.CON_RESOURCE_BOM_KEY)){ //合同提交优秀案例流程
            ConExcellentCases conExcellentCases = conExcellentCasesService.getAllByConId(Long.parseLong(bussinedId));
            conExcellentCases.setStatus(result);
            conExcellentCases.setArchiveTime(new Date());
            conExcellentCases.setApprovedBy(SecurityUtils.getUsername());
            conExcellentCasesService.updateById(conExcellentCases);
            switch (conExcellentCases.getTypeOfContract()){
                case 8:
                    ConResourceMemorandum conResourceMemorandum = conResourceMemorandumService.getById(bussinedId);
                    if(result==1){
                        conResourceMemorandum.setExcellent(2);
                    }else if(result==2){
                        conResourceMemorandum.setExcellent(3);
                    }else {
                        conResourceMemorandum.setExcellent(0);
                    }
                    conResourceMemorandumService.updateById(conResourceMemorandum);
                    break;
                case 2:
                   ConShipping conShipping = conShippingService.getById(bussinedId);
                   if(result==1){
                       conShipping.setExcellent(2);
                   }else if(result==2){
                       conShipping.setExcellent(3);
                   }else {
                       conShipping.setExcellent(0);
                   }
                   conShippingService.updateById(conShipping);
                   break;
                case 3:
                    ConShippingAgency conShippingAgency = conShippingAgencyService.getById(bussinedId);
                    if(result==1){
                        conShippingAgency.setExcellent(2);
                    }else if(result==2){
                        conShippingAgency.setExcellent(3);
                    }else {
                        conShippingAgency.setExcellent(0);
                    }
                    conShippingAgencyService.updateById(conShippingAgency);
                    break;
                case 10:
                    ConBroker conBroker = conBrokerService.getById(bussinedId);
                    if(result==1){
                        conBroker.setExcellent(2);
                    }else if(result==2){
                        conBroker.setExcellent(3);
                    }else {
                        conBroker.setExcellent(0);
                    }
                    conBrokerService.updateById(conBroker);
                    break;
                case 4:
                    ConThirdPartyMeasurement conThirdPartyMeasurement = conThirdPartyMeasurementService.getById(bussinedId);
                    if(result==1){
                        conThirdPartyMeasurement.setExcellent(2);
                    }else if(result==2){
                        conThirdPartyMeasurement.setExcellent(3);
                    }else {
                        conThirdPartyMeasurement.setExcellent(0);
                    }
                    conThirdPartyMeasurementService.updateById(conThirdPartyMeasurement);
                    break;
                case 5:
                    ConInsuranceGoods insuranceGoods = conInsuranceGoodsService.getById(bussinedId);
                    if(result==1){
                        insuranceGoods.setExcellent(2);
                    }else if(result==2){
                        insuranceGoods.setExcellent(3);
                    }else {
                        insuranceGoods.setExcellent(0);
                    }
                    conInsuranceGoodsService.updateById(insuranceGoods);
                    break;
                case 6:
                    ConRefueling conRefueling = conRefuelingService.getById(bussinedId);
                    if(result==1){
                        conRefueling.setExcellent(2);
                    }else if(result==2){
                        conRefueling.setExcellent(3);
                    }else {
                        conRefueling.setExcellent(0);
                    }
                    conRefuelingService.updateById(conRefueling);
                    break;
                case 7:
                    ConLawyer conLawyer = conLawyerService.getById(bussinedId);
                    if(result==1){
                        conLawyer.setExcellent(2);
                    }else if(result==2){
                        conLawyer.setExcellent(3);
                    }else {
                        conLawyer.setExcellent(0);
                    }
                    conLawyerService.updateById(conLawyer);
                    break;
                case 9:
                    ConInsurancePeople conInsurancePeople = conInsurancePeopleService.getById(bussinedId);
                    if(result==1){
                        conInsurancePeople.setExcellent(2);
                    }else if(result==2){
                        conInsurancePeople.setExcellent(3);
                    }else {
                        conInsurancePeople.setExcellent(0);
                    }
                    conInsurancePeopleService.updateById(conInsurancePeople);
                    break;
                case 1:
                    ConResourceMain conResourceMain = conResourceMainService.getById(bussinedId);
                    if(result==1){
                        conResourceMain.setExcellent(2);
                    }else if(result==2){
                        conResourceMain.setExcellent(3);
                    }else {
                        conResourceMain.setExcellent(0);
                    }
                    conResourceMainService.updateById(conResourceMain);
                    break;
                default:
            }
        }else if(bpmKey.equals(BpmInstanceService.CON_TEMPLATE_BPM_KEY)){ //提交合同模版
            ConTemplate conTemplate = conTemplateService.getById(bussinedId);
            conTemplate.setStatus(result);
            conTemplate.setArchiveTime(new Date());
            conTemplate.setApprovedBy(SecurityUtils.getUsername());
            conTemplateService.updateById(conTemplate);
        }else if(bpmKey.equals(BpmInstanceService.SAVE_CLAUSE_BPM_KEY)){//新增合同条款
            ConTemplateClause conTemplateClause = conTemplateClauseService.getById(bussinedId);
            conTemplateClause.setStatus(result);
            conTemplateClauseService.updateById(conTemplateClause);
        }

    }

    @Override
    @Transactional
    public void deleteConResult(String bussinedId, Integer result, String bpmKey, Integer serveType) {
        System.out.println(serveType);
        switch (serveType){
            case 8:
                ConResourceMemorandum conResourceMemorandum = conResourceMemorandumService.getById(bussinedId);
                if(result==1){
                    conResourceMemorandum.setDelApply(2);
                }else{
                    conResourceMemorandum.setDelApply(3);
                }
                conResourceMemorandumService.updateById(conResourceMemorandum);
                break;
            case 2:
                ConShipping conShipping = conShippingService.getById(bussinedId);
                if(result==1){
                    conShipping.setDelApply(2);
                }else{
                    conShipping.setDelApply(3);
                }
                conShippingService.updateById(conShipping);
                break;
            case 3:
                ConShippingAgency conShippingAgency = conShippingAgencyService.getById(bussinedId);
                if(result==1){
                    conShippingAgency.setDelApply(2);
                }else{
                    conShippingAgency.setDelApply(3);
                }
                conShippingAgencyService.updateById(conShippingAgency);
                break;
            case 10:
                ConBroker conBroker = conBrokerService.getById(bussinedId);
                if(result==1){
                    conBroker.setDelApply(2);
                }else{
                    conBroker.setDelApply(3);
                }
                conBrokerService.updateById(conBroker);
                break;
            case 4:
                ConThirdPartyMeasurement conThirdPartyMeasurement = conThirdPartyMeasurementService.getById(bussinedId);
                if(result==1){
                    conThirdPartyMeasurement.setDelApply(2);
                }else{
                    conThirdPartyMeasurement.setDelApply(3);
                }
                conThirdPartyMeasurementService.updateById(conThirdPartyMeasurement);
                break;
            case 5:
                ConInsuranceGoods insuranceGoods = conInsuranceGoodsService.getById(bussinedId);
                if(result==1){
                    insuranceGoods.setDelApply(2);
                }else{
                    insuranceGoods.setDelApply(3);
                }
                conInsuranceGoodsService.updateById(insuranceGoods);
                break;
            case 6:
                ConRefueling conRefueling = conRefuelingService.getById(bussinedId);
                if(result==1){
                    conRefueling.setDelApply(2);
                }else{
                    conRefueling.setDelApply(3);
                }
                conRefuelingService.updateById(conRefueling);
                break;
            case 7:
                ConLawyer conLawyer = conLawyerService.getById(bussinedId);
                if(result==1){
                    conLawyer.setDelApply(2);
                }else{
                    conLawyer.setDelApply(3);
                }
                conLawyerService.updateById(conLawyer);
                break;
            case 9:
                ConInsurancePeople conInsurancePeople = conInsurancePeopleService.getById(bussinedId);
                if(result==1){
                    conInsurancePeople.setDelApply(2);
                }else{
                    conInsurancePeople.setDelApply(3);
                }
                conInsurancePeopleService.updateById(conInsurancePeople);
                break;
            case 1:
                ConResourceMain conResourceMain = conResourceMainService.getById(bussinedId);
                if(result==1){
                    conResourceMain.setDelApply(2);
                }else{
                    conResourceMain.setDelApply(3);
                }
                conResourceMainService.updateById(conResourceMain);
                break;
            default:
        }
    }
}
